package com.sfh.base.ui.refresh;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.support.annotation.AttrRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sfh.base.R;

import java.util.List;

/**
 * 功能描述:刷新加载更多控件
 *
 * @author SunFeihu 孙飞虎
 * @company 中储南京智慧物流科技有限公司
 * @copyright （版权）中储南京智慧物流科技有限公司所有
 * @date 2017/7/12
 */
public class SwipeRefreshMoreLayout extends FrameLayout implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    protected SwipeRefreshLayout mSwipeRefreshLayout;//刷新控件

    protected RecyclerView mRecyclerView; // 视图显示控件

    protected BaseQuickAdapter pullToRefreshAdapter;// 适配器

    protected OnLoadingListener mListener;//加载更多与刷新回调

    protected long time = 250L;//延迟刷新时间【毫秒】

    public interface OnLoadingListener {

        /***刷新*/
        void onRefreshUI();

        /***加载更多*/
        void onLoadMoreUI();
    }

    public SwipeRefreshMoreLayout(@NonNull Context context) {

        super (context);
        this.init ();
    }

    public SwipeRefreshMoreLayout(@NonNull Context context, @Nullable AttributeSet attrs) {

        super (context, attrs);
        this.init ();
    }

    public SwipeRefreshMoreLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {

        super (context, attrs, defStyleAttr);
        this.init ();
    }

    private void init() {

        this.inflate (getContext (), R.layout.base_refresh_more_layout, this);
        this.mSwipeRefreshLayout = $ (R.id.base_refresh);
        this.mRecyclerView = $ (R.id.base_list);
    }

    /***
     * 设置适配器
     * @param adapter
     * @param loadMore true 表示刷新+加载更多 false 只有刷新功能
     */
    public void setAdapter(BaseQuickAdapter adapter, boolean loadMore) {

        this.pullToRefreshAdapter = adapter;


        mSwipeRefreshLayout.setColorSchemeColors (Color.parseColor ("#3c75ed"));
        mSwipeRefreshLayout.setOnRefreshListener (this);// 刷新
        mRecyclerView.addItemDecoration (new RecyclerView.ItemDecoration () {

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

                if (parent.getChildPosition (view) != -1)
                    outRect.top = 15;
            }
        });
        mRecyclerView.setLayoutManager (new LinearLayoutManager (getContext ()));//线性
        pullToRefreshAdapter.openLoadAnimation (BaseQuickAdapter.SLIDEIN_BOTTOM); // 加载动画
        pullToRefreshAdapter.bindToRecyclerView (mRecyclerView);                  // 先绑定RecyclerView
        pullToRefreshAdapter.setEmptyView (R.layout.base_list_empty_view);        // 先绑定RecyclerView 没有数据的时候默认显示该布局

        if (loadMore) {
            // 加载更多
            pullToRefreshAdapter.setOnLoadMoreListener (this, mRecyclerView);
            pullToRefreshAdapter.disableLoadMoreIfNotFullPage (); // 默认不加载更多
//            pullToRefreshAdapter.setPreLoadNumber (1);// 当列表滑动到倒数第N个Item的时候(默认是1)回调onLoadMoreRequested方法
            pullToRefreshAdapter.setLoadMoreView (new CustomLoadMoreView ());//自定义加载布局
        }

    }

    private <T extends View> T $(@IdRes int resId) {

        return (T) super.findViewById (resId);
    }

    @Override
    public void onRefresh() {

        pullToRefreshAdapter.setEnableLoadMore (false);
        if (mListener != null) {
            mListener.onRefreshUI ();
        }
    }

    @Override
    public void onLoadMoreRequested() {

        if (mListener != null) {
            mListener.onLoadMoreUI ();
        }
    }

    /****
     * 刷新+加载更多回调
     * @param mListener
     */
    public void setOnLoadingListener(OnLoadingListener mListener) {

        this.mListener = mListener;
    }

    /***
     * item view 中控件点击事件
     * @param listener
     */
    public void setOnItemChildClickListener(BaseQuickAdapter.OnItemChildClickListener listener) {

        pullToRefreshAdapter.setOnItemChildClickListener (listener);//子控件点击事件
    }

    /*** item 点击事件 */
    public void setOnItemClickListener(@Nullable BaseQuickAdapter.OnItemClickListener listener) {

        pullToRefreshAdapter.setOnItemClickListener (listener); //Item 点击事件
    }

    /***
     * 是否全部加载完成 【外部调用】
     */
    public void onLoadAllCompale() {

        pullToRefreshAdapter.loadMoreEnd ();
        this.closeRefresh ();
    }


    /***
     * 获取更多数据失败 【外部调用】
     *
     */
    public void onLoadMoreFail() {

        pullToRefreshAdapter.loadMoreFail ();
        this.closeRefresh ();
    }

    /***
     * 完成数据加载【外部调用】
     *
     * @param data 数据
     * @param loadMore true 加载更多完成 false 刷新完成
     */
    public void onRefreshCompale(final List data, boolean loadMore) {

        if (loadMore) {
            //成功获取更多数据
            pullToRefreshAdapter.addData (data);
            pullToRefreshAdapter.loadMoreComplete ();
        }else{
            pullToRefreshAdapter.setNewData (data);
            pullToRefreshAdapter.setEnableLoadMore (true);
           this.closeRefresh();
        }

    }

    // 关闭刷新
    private void closeRefresh() {

        this.postDelayed (new Runnable () {

            @Override
            public void run() {

                SwipeRefreshMoreLayout.this.removeCallbacks (this);
                mSwipeRefreshLayout.setRefreshing (false);
            }
        }, time);
    }

    @Override
    protected void onDetachedFromWindow() {

        this.removeCallbacks (null);
        super.onDetachedFromWindow ();
    }

    /****
     * 自动刷新【外部调用】
     */
    public void onAutoRefresh() {

        this.postDelayed (new Runnable () {

            @Override
            public void run() {

                SwipeRefreshMoreLayout.this.removeCallbacks (this);
                mSwipeRefreshLayout.setRefreshing (true);
                if (mListener != null) {
                    mListener.onRefreshUI ();
                }
            }
        }, time);
    }
}
